Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			{name:'id',type:'string'},
            {name:'base_salary',type:'float'},
            {name:'performance_salary',type:'float'},
            {name:'secrecy_salary',type:'float'},
            {name:'traffic_subsidy',type: 'float'},
            {name:'comm_subsidy',type:'float'},
            {name:'amount',type:'float'},
            {name:'salary_level',type:'string'},
            {name:'salary_grade',type:'string'},
            {name:'position_level',type:'string'},
            {name:'remark',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
        groupField: 'salary_grade',
	    proxy: {
	        type: 'ajax',
	        url: '/resourcecontrol/salarylevel_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });   
    
	var data123=[
		{'id':'1','value':'一级'},
		{'id':'2','value':'二级'},
		{'id':'3','value':'三级'},
		{'id':'4','value':'四级'},
		{'id':'5','value':'五级'}
	]
	
	var data3=[
		{'id':'1','value':'一级'},
		{'id':'2','value':'二级'},
		{'id':'3','value':'三级'},
		{'id':'4','value':'四级'},
		{'id':'5','value':'五级'},
		{'id':'6','value':'六级'}
	]
    
	var data4=[
		{'id':'1','value':'一级'},
		{'id':'2','value':'二级'},
		{'id':'3','value':'三级'},
		{'id':'4','value':'四级'},
		{'id':'5','value':'五级'},
		{'id':'6','value':'六级'},
		{'id':'7','value':'七级'},
		{'id':'8','value':'八级'},
		{'id':'9','value':'九级'},
		{'id':'10','value':'十级'},
		{'id':'11','value':'十一级'},
		{'id':'12','value':'十二级'},
		{'id':'13','value':'十三级'},
		{'id':'14','value':'十四级'},
		{'id':'15','value':'十五级'}
	]
			
	// 兼职
	var data5=[
		{'id':'1','value':'一级'},
		{'id':'2','value':'二级'},
		{'id':'3','value':'三级'},
		{'id':'4','value':'四级'},
		{'id':'5','value':'五级'},
		{'id':'6','value':'六级'},
		{'id':'7','value':'七级'},
		{'id':'8','value':'八级'},
		{'id':'9','value':'九级'},
		{'id':'10','value':'十级'},
		{'id':'11','value':'十一级'},
		{'id':'12','value':'十二级'},
		{'id':'13','value':'十三级'},
		{'id':'14','value':'十四级'},
		{'id':'15','value':'十五级'},
		{'id':'16','value':'十六级'},
		{'id':'17','value':'十七级'},
		{'id':'18','value':'十八级'},
		{'id':'19','value':'十九级'},
		{'id':'20','value':'二十级'},
		{'id':'21','value':'二十一级'},
		{'id':'22','value':'二十二级'}
	]
	

    var levelComboBox= Ext.create('Ext.form.field.ComboBox', {
			xtype:'combobox',
			editable:false,
			queryMode: 'local',
			valueField:'id',
			displayField:'value',
			fieldLabel : '等级',
			name : 'position_level',
			id : 'position_level',
			allowBlank : true,
			blankText : '请输入',
			store: Ext.create('Ext.data.Store',{
				autoLoad:true,
				fields:['id','value'], 
				data: data5
			}),
			columnWidth: 1/2
		});
		
    var gradeComboBox= Ext.create('Ext.form.field.ComboBox',{
			xtype:'combobox',
			editable:false,
			valueField:'id',
			displayField:'value',
			queryMode:'local',
			fieldLabel : '薪档',
			name : 'salary_grade',
			id : 'salary_grade',
			allowBlank : true,
			blankText : '请输入',
			store:Ext.create('Ext.data.Store',{
				fields:['id','value'], 
				data:[
					{'id':'1','value':'XD(A)'},
					{'id':'2','value':'XD(B)'},
					{'id':'3','value':'XD(C)'},
					{'id':'4','value':'XD(D)'},
					{'id':'5','value':'JZXD(E)'}
				]
			}),
			columnWidth: 1/2,
			listeners: {
        		'select': function(combo, records, opts) {	
        			if( gradeComboBox.getValue()=='4' ){
        				levelComboBox.getStore().loadData(data4);
        			}else if(gradeComboBox.getValue()=='3'){
        				levelComboBox.getStore().loadData(data3);
        			}else if(gradeComboBox.getValue()=='5'){
        				levelComboBox.getStore().loadData(data5);
        			}else{
        				levelComboBox.getStore().loadData(data123);
        			}
        		}
        	}
	
		} );
    
    
    
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        selModel:selModel,
        columnLines:true,
        features: [{
        	ftype: 'groupingsummary',
        	groupHeaderTpl: '{name}'
        }],
        columns: [
			Ext.create('Ext.grid.RowNumberer'),
            {text: "基本工资", width: 100, dataIndex: 'base_salary',groupable: false,renderer: Ext.util.Format.usMoney},
            {text: "绩效工资", width: 100, dataIndex: 'performance_salary',groupable: false, renderer: Ext.util.Format.usMoney},
            {text: "保密工资", width: 100, dataIndex: 'secrecy_salary',groupable: false, renderer: Ext.util.Format.usMoney},
            {text: "交通补贴", width: 100, dataIndex: 'traffic_subsidy',groupable: false, renderer: Ext.util.Format.usMoney},
            {text: "通讯补贴", width: 100, dataIndex: 'comm_subsidy',groupable: false, renderer: Ext.util.Format.usMoney},
            {text: "合计总额", width: 100, dataIndex: 'amount',groupable: false, sortable: false,
                renderer: function(value, metaData, record, rowIdx, colIdx, store, view) {
                    return Ext.util.Format.usMoney(parseFloat(record.get('base_salary'))+parseFloat(record.get('performance_salary'))+
                    		parseFloat(record.get('secrecy_salary'))+parseFloat(record.get('traffic_subsidy'))+parseFloat(record.get('comm_subsidy')));
                }
            },
            {text: "职位等级", width: 100, dataIndex: 'position_level', sortable: false,groupable: false,
            	 renderer:function(v){
	            	if (v==1) 
	                	return '一级' ;
	            	if (v==2) 
	            		return '二级' ;
	        		if (v==3) 
	        			return '三级' ;
	    			if (v==4) 
	    				return '四级' ;
					if (v==5) 
						return '五级' ;
					if (v==6) 
						return '六级' ;
					if (v==7) 
						return '七级' ;
					if (v==8)
						return '八级' ;
					if (v==9) 
						return '九级' ;
					if (v==10) 
						return '十级' ;
					if (v==11) 
						return '十一级' ;
					if (v==12) 
						return '十二级' ;
					if (v==13) 
						return '十三级' ;
					if (v==14) 
						return '十四级' ;
					if (v==15) 
						return '十五级' ;
					if (v==16) 
						return '十六级' ;
					if (v==17) 
						return '十七级' ;
					if (v==18) 
						return '十八级' ;
					if (v==19) 
						return '十九级' ;
					if (v==20) 
						return '二十级' ;
					if (v==21) 
						return '二十一级' ;
					if (v==22) 
						return '二十二级' ;
					
				
            	}
            },
            {text: "备注", width: 120, dataIndex: 'remark', sortable: false,groupable: false}
        ],
        //title: '薪资等级',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
        region: 'north',
        //renderTo: 'salarylevel-grid',
        viewConfig: {
            stripeRows: true
        },
        dockedItems : [{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			form.getForm().reset();
        			var selected = grid.getSelectionModel().getSelection();
					form.loadRecord(selected[0]);
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id+",";
        			});
        			Ext.Ajax.request({
        			    url:'/resourcecontrol/salarylevel_del/',
        			    method : 'post',
        			    params:{
        			    	ids:ids
        			    },
        			    success:function(response){
        			        var results =  Ext.decode(response.responseText);
        			        if(results.success){
        			        	store.load();
        			        }else{
        			        	Ext.Msg.alert('系统提示', results.message);
        			        }
        			    }
        			});
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		width: 550,
		bodyPadding: 10,
        fieldDefaults: {
            //labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'column',
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'textfield',
			fieldLabel:'基本工资',
			name:'base_salary',
			columnWidth: 1/2,
			allowBlank: false
		},{
			xtype:'textfield',
			fieldLabel:'绩效工资',
			name:'performance_salary',
			columnWidth: 1/2
		},{
			xtype:'textfield',
			fieldLabel:'保密工资',
			name:'secrecy_salary',
			columnWidth: 1/2,
			allowBlank: false
		},{
			xtype:'textfield',
			fieldLabel:'交通补贴',
			name:'traffic_subsidy',
			columnWidth: 1/2,
			allowBlank: false
		},{
			xtype:'textfield',
			fieldLabel:'通讯补贴',
			name:'comm_subsidy',
			columnWidth: 1/2
		},gradeComboBox,levelComboBox,{
			xtype:'textfield',
			fieldLabel:'备注',
			name:'remark',
			columnWidth: 1/1
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/resourcecontrol/salarylevel_save/',
						success : function(form, action) {
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'salarylevel-grid',
		items:[grid]
	});
	
});